package com.chuanghai.light;

import com.chuanghai.light.service.WebSocketServer;
import com.chuanghai.light.tcp.TCPServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.IOException;
import java.util.HashMap;
import java.util.Set;

@SpringBootApplication
public class LightControlApplication {
    static TCPServer tcpServer=null;

    public static void main(String[] args) {
        SpringApplication.run(LightControlApplication.class, args);

        // 检查是否掉线
        new Thread(new Runnable() {
            @Override
            public void run() {
                while(true)
                {
                    if(tcpServer.wakeUpTime.size()>0) {
                        for (HashMap.Entry<String, Integer> entry : tcpServer.wakeUpTime.entrySet()) {
                            int time = entry.getValue();
                            System.out.println("key = " + entry.getKey() + ", time = " + time);
                            if(time>80)  // 超过80秒没消息过来表示掉线了
                            {
                                System.out.println(entry.getKey()+" is outline");
                                try {
                                    tcpServer.removeSocket(entry.getKey());
                                    tcpServer.mysqlTool.updateOnlineByPointCode(entry.getKey(), false);  // 修改网络状态
                                    WebSocketServer.BroadCastInfo("{\"pointCode\":\""+entry.getKey()+"\",\"online\":0}");
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                            else{
                                time+=20;
                                entry.setValue(time);
                            }
                        }
                    }
                    try {
                        Thread.sleep(20000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        // 路灯TCP连接
        while (true) {
            tcpServer = new TCPServer();
            tcpServer.startRun(7003);
            tcpServer.setOnRequestListener(new TCPServer.OnRequestListener() {
                @Override
                public String onRequestListener(String request) {
                    //System.out.println("收到的客户端请求:  " + request);
                    return request;
                }
            });

            tcpServer.setOnResponseListener(new TCPServer.OnResponseListener() {
                @Override
                public String onResponseListener(String response) {
                    // System.out.println("收到的网关响应:  " + response);
                    return response;
                }
            });
            tcpServer.setOnSocketChange(new TCPServer.OnSocketChange() {
                @Override
                public void onSocketChange(Set<String> devs) {
                    System.out.print("List of online clients:  ");
                    for (String dev : devs) {
                        System.out.print(dev + "    ");
                    }
                    System.out.println();
                }
            });
            tcpServer.start();
            System.out.println("Service started");
            tcpServer.waitServerStop();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("The service is abnormally shut down and being restarted...");
        }
    }

}
